/*
 * DiscoverySearchPresenter.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.search;

import com.yidiandao.business.data.model.IArticleModel;
import com.yidiandao.business.data.model.IDiscoverySearchModel;
import com.yidiandao.business.data.recyclertype.SearchAccurateContent;
import com.yidiandao.business.data.recyclertype.SearchHistory;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现页-搜索-一级页面
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/12 0012 18:28
 */
public class DiscoverySearchPresenter implements DiscoverySearchContact.Presenter {

    private DiscoverySearchContact.View mSearchView;

    private IDiscoverySearchModel mSearchModel;

    private List<Object> mDisplayItems;

    public DiscoverySearchPresenter(DiscoverySearchContact.View searchView, IDiscoverySearchModel searchModel, IArticleModel articleModel) {

        mSearchView = searchView;
        mSearchModel = searchModel;

        mDisplayItems = new ArrayList<>();

        mSearchView.setPresenter(this);
    }

    @Override
    public void start() {
        mDisplayItems.clear();
        final SearchHistory searchHistory = mSearchModel.loadSearchHistory();
        mSearchModel.loadAccurateContent(mSearchView.getPagerTag(), new IDiscoverySearchModel.OnLoadResultListener<SearchAccurateContent>() {
            @Override
            public void onSuccess(SearchAccurateContent accurateContents, boolean emptyData, boolean hasMore) {

                mDisplayItems.add(accurateContents);
                if (searchHistory != null) {
                    mDisplayItems.add(searchHistory);
                }

                mSearchView.refreshContent(mSearchView.checkResultObject(mDisplayItems));

                mSearchView.setItems(mDisplayItems);
            }

            @Override
            public void onFailed() {
                mSearchView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void search(String keyword) {

        mDisplayItems.clear();
        mSearchView.setItems(mDisplayItems);

        mSearchModel.commonSearch(mSearchView.getPagerTag(), keyword, new IDiscoverySearchModel.OnLoadResultListener<List<Object>>() {
            @Override
            public void onSuccess(List<Object> objects, boolean emptyData, boolean hasMore) {
                mSearchView.refreshContent(mSearchView.checkResultObject(objects));
                mSearchView.setItems(objects);
            }

            @Override
            public void onFailed() {
                mSearchView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void clearHistory() {
        try {
            mSearchModel.clearHistory();
            if (mDisplayItems.get(1) instanceof SearchHistory) {
                mDisplayItems.remove(1);
                mSearchView.setItems(mDisplayItems);
            }
        } catch (IndexOutOfBoundsException e) {
            // 清空历史
        }
    }
}
